<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="uname"
        ><br />
        <el-input v-model="ruleForm.uname"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password"
        ><br />
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deng">登录</el-button>
        <el-button @click="submitForm('ruleForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        uname: "",
        password: "",
      },
      rules: {
        uname: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证通过之后
          this.$axios
            .post("http://localhost:55943//api/Regiter", this.ruleForm)
            .then((res) => {
              if (res.data > 0) {
                this.$message.success("注册成功");
                this.$router.push("/login");
              } else {
                this.$message.error("注册失败");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deng() {
      this.$router.push("/login");
    },
  },
};
</script>

<style>
</style>